package com.vn.toaa.karaoke.ui;

import java.io.IOException;

import android.app.Activity;
import android.app.AlertDialog.Builder;
import android.content.DialogInterface;
import android.content.DialogInterface.OnClickListener;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;

import com.startapp.android.publish.StartAppAd;
import com.vn.toaa.karaoke.database.DatabaseManager;
import com.vn.toaa.karaoke.database.ExternalSQLiteHelper;
import com.vn.toaa.karaokecodelist.R;

public class SplashActivity extends Activity {
	
	public static final String TAG = SplashActivity.class.getSimpleName();
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		
		StartAppAd.init(getApplicationContext(), Constant.STARTAPP_DEV_ID, Constant.STARTAPP_APP_ID);
		
		setContentView(R.layout.splash_activity);
		Log.i(TAG, "onCreate called");
		
		final ProgressBgView prog = (ProgressBgView) findViewById(R.id.progress_db_view);
		prog.setBackgroundAsTile(R.drawable.bg_progress_bar);
		prog.startAnimation();
		
		new Thread(new Runnable() {
			public void run() {
		        ExternalSQLiteHelper.createInstance(getApplicationContext(), Constant.DATABASE_NAME, Constant.DATABASE_PATH, Constant.DATABASE_VERSION);
		        try {
					ExternalSQLiteHelper.getInstance().createDatabase();
				} catch (IOException e) {
					e.printStackTrace();
					runOnUiThread(new Runnable() {
						public void run() {
							displayErrorDialog();
						}
					});
				}
		        
		        DatabaseManager.getInstance().openConnection();
		        ValueContainer.getInstance().setSongModelsInFive(DatabaseManager.getInstance().selectAllSongInFive());
		        ValueContainer.getInstance().setSongModelsInSix(DatabaseManager.getInstance().selectAllSongInSix());
		        if (ValueContainer.getInstance().getSongModelsInFive() == null || ValueContainer.getInstance().getSongModelsInFive().size() <= 0
		        		|| ValueContainer.getInstance().getSongModelsInSix() == null || ValueContainer.getInstance().getSongModelsInSix().size() <= 0) {
					runOnUiThread(new Runnable() {
						public void run() {
							displayErrorDialog();
						}
					});
				} else {
					runOnUiThread(new Runnable() {
						public void run() {
							startActivity(new Intent(SplashActivity.this, MainActivity.class));
							finish();
						}
					});
				}
			}
		}).start();
	}
	
	@Override
	protected void onDestroy() {
		DatabaseManager.getInstance().closeConnection();
		super.onDestroy();
	}
	
	private void displayErrorDialog() {
		final Builder builder = new Builder(this);
		builder.setTitle("Error");
		builder.setIcon(android.R.drawable.ic_dialog_alert);
		builder.setMessage("Cannot connect to database due to unknown reasons.");
		builder.setPositiveButton("OK", new OnClickListener() {
			
			@Override
			public void onClick(DialogInterface dialog, int which) {
				dialog.dismiss();
				finish();
			}
		});
		builder.create().show();
	}
}
